package com.itheima.exerciseTest.method30_39.method31;

import java.util.ArrayList;
import java.util.concurrent.Semaphore;
import java.util.concurrent.atomic.AtomicInteger;

// 顾客类
public class Foodie implements Runnable {
    private Storefront sf;

    public Foodie(Storefront sf) {
        this.sf = sf;
    }

    private Semaphore sh = new Semaphore(3);

    @Override
    public void run() {
        while (true) {
            synchronized (sf){
                if (sf.getCount() >= sf.getNumber()) {
                    break;
                }
                // 判断桌子上还有没有蛋糕
                if (sf.getList().size() <= 0) {
                    // 没有蛋糕了就叫厨师来做
                    sf.notifyAll();
                } else {
                    // 有蛋糕就吃
                    System.out.println(Thread.currentThread().getName() + "号顾客正在蛋糕");
                    ArrayList<Integer> list = sf.getList();
                    list.remove(0);
                    sf.setList(list);
                    try {
                        Thread.sleep(10);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }

        }
    }
}
